﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace GMS.BasicControls
{
    public partial class ColorPicker : BackGroundControl
    {
        public ColorPicker()
        {
            InitializeComponent();
            dataGridView1.AutoGenerateColumns = false;
        }
        private DataSet _colorPickerDataset;
        public DataSet ColorPickerDataset
        {
            get { return _colorPickerDataset; }
            set { _colorPickerDataset = value; }
        }
        private string selectSuffusionCommand;
        public string SelectSuffusionCommand
        {
            get { return selectSuffusionCommand; }
            set { selectSuffusionCommand = value; }
        }
        private string selectSuffusion_ColorCommand;
        public string SelectSuffusion_ColorCommand
        {
            get { return selectSuffusion_ColorCommand; }
            set { selectSuffusion_ColorCommand = value; }
        }
        private string selectColorCommand;
        public string SelectColorCommand
        {
            get { return selectColorCommand; }
            set { selectColorCommand = value; }
        }
        private string connectionString;
        public string ConnectionString
        {
            get { return connectionString; }
            set { connectionString = value; }
        }
        private void ColorPicker_Load(object sender, EventArgs e)
        {
            SqlConnection connection = new SqlConnection();
            connection.ConnectionString = connectionString;

            connection.Open();

            dataAdapter.SelectCommand = new SqlCommand(selectSuffusionCommand);
            dataAdapter.SelectCommand.Connection = connection;
            dataAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;
            dataAdapter.Fill(datasetColorPicker, "suffusion");

            dataAdapter.SelectCommand = new SqlCommand(selectSuffusion_ColorCommand);
            dataAdapter.SelectCommand.Connection = connection;
            dataAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;
            dataAdapter.Fill(datasetColorPicker, "suffusion_colors");

            dataAdapter.SelectCommand = new SqlCommand(selectColorCommand);
            dataAdapter.SelectCommand.Connection = connection;
            dataAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;
            dataAdapter.Fill(datasetColorPicker, "colors");

            CreateDataset();

            comboBox1.DataSource = datasetColorPicker.Tables["suffusion"];
            comboBox1.ValueMember = "Id";
            comboBox1.DisplayMember = "CaptionSuffusion";

            
            dtw = datasetColorPicker.Tables["colors"].DefaultView;
            dataGridView1.DataSource = dtw;
            FilterColor();
        }
        DataView dtw = new DataView();
        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            //_colors.Select(string.Format("CaptionColor = " + textBox1.Text.Trim()));
        }
        private SqlDataAdapter dataAdapter = new SqlDataAdapter();
        private DataTable suffusion = new DataTable("suffusion");
        private DataTable colors = new DataTable("colors");
        private DataTable suffusion_colors = new DataTable("suffusion_colors");

        private DataSet datasetColorPicker = new DataSet("datasetColorPicker");
        private void CreateDataset()
        {
            DataRelation dr = new DataRelation("suffusion_color", datasetColorPicker.Tables["suffusion"].Columns["Id"], datasetColorPicker.Tables["suffusion_colors"].Columns["IdSuffusion"]);
            datasetColorPicker.Relations.Add(dr);
            dr = new DataRelation("color_suffusion", datasetColorPicker.Tables["suffusion"].Columns["Id"], datasetColorPicker.Tables["suffusion_colors"].Columns["IdColorRecord"]);
            datasetColorPicker.Relations.Add(dr);
        }
        private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
        {
            FilterColor();
        }
        private void FilterColor()
        {
            dataGridView1.Columns["RRColor"].Visible = true;
            dataGridView1.Columns["RALColor"].Visible = true;
            dataGridView1.Columns["ShinglasColor"].Visible = true;
            //datasetColorPicker.Tables["colors"].Rows.Clear();
            int currentSuffusion = comboBox1.SelectedIndex;
            DataRow drSuffusion = datasetColorPicker.Tables["suffusion"].Rows[currentSuffusion];
            DataRow[] drAvailableColors = drSuffusion.GetChildRows(datasetColorPicker.Relations["suffusion_color"]);
            string idColors = "0";
            int RRColumn = 0, RALColumn = 0, ShainglasColumn = 0;
            for (int i = 0; i < drAvailableColors.Length; i++)
            {
                idColors = idColors + "," + drAvailableColors[i]["IdColorRecord"].ToString();
                RRColumn = RRColumn + int.Parse(drAvailableColors[i]["RRColumn"].ToString());
                RALColumn = RALColumn + int.Parse(drAvailableColors[i]["RALColumn"].ToString());
                ShainglasColumn = ShainglasColumn + int.Parse(drAvailableColors[i]["ShinglasColumn"].ToString());
            }

            if (RRColumn == 0)
                dataGridView1.Columns["RRColor"].Visible = false;
            if (RALColumn == 0)
                dataGridView1.Columns["RALColor"].Visible = false;
            if (ShainglasColumn == 0)
                dataGridView1.Columns["ShinglasColor"].Visible = false;
            idColors = "(" + idColors + ")";
            string expression = string.Format("Id IN " + idColors);
            dtw.RowFilter = expression;
        }

        public string NewSuffusion
        {
            get 
            {
                return comboBox1.Text;
            }
        }
        public string NewColor
        {
            get 
            {
                if (dataGridView1.Columns[dataGridView1.CurrentCell.ColumnIndex].Name == "CaptionColor")
                    return dataGridView1.CurrentCell.Value.ToString();
                else
                    return dataGridView1.CurrentCell.Value.ToString() +" " + dataGridView1[dataGridView1.Columns["CaptionColor"].Index, dataGridView1.CurrentRow.Index].Value.ToString();
            }
        }

        private void applyLinkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            closeLabel_LinkClicked(sender, e);
        }
    }
}
